const UserService = require("../service/UserService")
const JWT = require("../utils/JWT")
module.exports = {
    async getList(req, res) {
        var result = await UserService.getList()
        res.send(result)
    },
    async login(req, res, next) {
        var result = await UserService.login(req.body)
        if (result.length > 0) {
            let {_id,username,role} = result[0]
            let token = JWT.generate({ _id, username }, "1d")
            res.header("Authorization", token)
            res.send({
                ActionType:"ok",
                data: { _id, username, role }
            })
        }else{
            res.send({
                ActionType:"fail",
                data:"用户名或密码错误"
            })
        }
        
    },
    async addList(req, res, next) {
        var result = await UserService.addList(req.body)
        res.send(result)
    },
    async updataList(req, res, next) {
        var result = await UserService.updataList(req.params.id, req.body)
        res.send(result)
    }
    ,
    async deleteList(req, res, next) {
        var result = await UserService.deleteList(req.params.id)
        res.send(result)
    }
}